<?php
/*
Copyright (c) 2007, Bruce Weirdan <weirdan@gmail.com>

All rights reserved.

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions 
are met:

    * Redistributions of source code must retain the above copyright 
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above 
      copyright notice, this list of conditions and the following 
      disclaimer in the documentation and/or other materials provided 
      with the distribution.
    * Neither the name of the Bruce Weirdan nor the names of his  
      contributors may be used to endorse or promote products derived 
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**
 * Main svnbot class
 * 
 * @package SVNBot
 * @version $Id: Bot.php 22 2007-12-09 16:06:39Z weirdan $
 * @copyright 2006 Bruce Weirdan
 * @author Bruce Weirdan <weirdan@gmail.com> 
 * @license BSD {@link http://opensource.org/licenses/bsd-license.php}
 */

/**
 * jabber class 
 */
require_once 'class_Jabber.php';

/**
 * updater class 
 */
require_once 'SVNBot/SVN/Updater.php';

/**
 * logger class 
 */
require_once 'SVNBot/Logger/Debug.php';

/**
 * Main svnbot class
 * 
 * @package SVNBot
 */
class SVNBot_Bot
{
	/**
	 * __construct 
	 * 
	 * @param SVNBot_Config_Ini $config 
	 * @access public
	 * @return void
	 */
	public function __construct($config) 
	{
		$this->_config = $config;

		$this->_initUpdater($this->_config->repository, $this->_config->checkout);
		$this->_initJabber();

		// enter main loop
		$this->_jabber->connect($this->_config->server);
		$this->_jabber->execute(60);
	}

	/**
	 * _initUpdater 
	 * 
	 * @access private
	 * @return void
	 */
	private function _initUpdater() 
	{
		$this->_updater = new SVNBot_SVN_Updater($this->_config->repository, $this->_config->checkout, $this->_config->svnpath);
		$this->_updater->addLogger($this);
		$this->_updater->addLogger(new SVNBot_Logger_Debug);
	}

	/**
	 * _initJabber 
	 * 
	 * @access private
	 * @return void
	 */
	private function _initJabber() 
	{
		$this->_jabber = new Jabber(true);
		$this->_jabber->set_handler('connected', $this, 'connected');
		$this->_jabber->set_handler('authenticated', $this, 'authenticated');
		$this->_jabber->set_handler('heartbeat', $this, 'heartbeat');
		$this->_jabber->set_handler('message_normal', $this, 'message');
		$this->_jabber->set_handler('message_chat', $this, 'message');
		$this->_jabber->set_handler('msgevent_composing_stop', $this, 'dummy');
		$this->_jabber->set_handler('msgevent_composing_start', $this, 'dummy');
		$this->_jabber->set_handler('debug_log', new SVNBot_Logger_Debug, 'log');
	}

	public function dummy() 
	{

	}

	/**
	 * connected 
	 * 
	 * @access public
	 * @return void
	 */
	public function connected() 
	{
		$this->_jabber->login($this->_config->login, $this->_config->password);
	}

	/**
	 * authenticated 
	 * 
	 * @access public
	 * @return void
	 */
	public function authenticated() 
	{
		$this->_jabber->browse();
		$this->_jabber->get_roster();
		$this->_jabber->set_presence('', 'Ready to serve');
	}

	/**
	 * heartbeat 
	 * 
	 * @access public
	 * @return void
	 */
	public function heartbeat() 
	{
		$this->_updater->update();
	}

	/**
	 * log 
	 * 
	 * @param string $msg 
	 * @access public
	 * @return void
	 */
	public function log($msg) 
	{
		foreach ($this->_jabber->roster as $jid => $details) {
			$this->_jabber->message($jid, 'chat', null, $msg);
		}
	}

	/**
	 * message 
	 * 
	 * @param string $from 
	 * @param string $to 
	 * @param string $body 
	 * @param string $subject 
	 * @param mixed $thread 
	 * @param mixed $id 
	 * @param array $extended 
	 * @access public
	 * @return void
	 */
	public function message($from, $to, $body, $subject, $thread, $id, $extended) 
	{
		$msg = <<<MSG
Broadcast message from: $from
Subject: $subject

$body
MSG;
		foreach ($this->_jabber->roster as $jid => $details) {
			$this->_jabber->message($jid, 'chat', null, $msg);
		}
	}

	/**
	 * disconnected 
	 * 
	 * @access public
	 * @return void
	 */
	public function disconnected() 
	{
		$this->_jabber->connect($this->_config->server);
		$this->_jabber->execute(60);
	}
}
